using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using ViewOnCore.Helper.WPF;
using ViewOnCore.Helper.Collection;
using ViewOnCore.HostContract.Media;
using ViewOnCore.Device;
using ViewOnCore.HostContract;

namespace UI.Control
{
    /// <summary>
    /// Display information about a track
    /// </summary>
    public partial class VideoTagControl : System.Windows.Controls.UserControl
    {

        #region Variables

        private List<Video> _selectedVideos;
        private string _previousTextValue = null;
        private bool _isReadOnly = true;

        private ToggleButton[] _tbtRatings;
        private bool _isUpdating = false;

        private VideoIntellisenseManager _intellisenseManager = new VideoIntellisenseManager();

        #endregion

        #region Constructor

        public VideoTagControl()
        {
            InitializeComponent();

            //---- Media kernel events
            Host.MediaService.MediaDataOperation += new MediaDataOperationEventHandler(MediaKernel_MediasOperation);

            //imgFrontCover.Drop += new DragEventHandler(imgFrontCover_Drop);
            //imgFrontCover.DragOver += new DragEventHandler(imgFrontCover_DragOver);
            //imgFrontCoverBorder.Drop += new DragEventHandler(imgFrontCover_Drop);
            //imgFrontCoverBorder.DragOver += new DragEventHandler(imgFrontCover_DragOver);
            //imgFrontCover.MouseLeftButtonUp += new MouseButtonEventHandler(imgFrontCover_MouseLeftButtonUp);
            //imgFrontCoverHighlight.MouseLeftButtonUp += new MouseButtonEventHandler(imgFrontCover_MouseLeftButtonUp);

            txtMetaTags.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtTitle.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtGenres.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtDuration.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtDirector.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtPlotOutline.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtTitle.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtReleaseDate.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtLangages.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtTraillers.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtCompanies.LostFocus += new RoutedEventHandler(control_LostFocus);
            txtCasts.LostFocus += new RoutedEventHandler(control_LostFocus);

            txtMetaTags.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtTitle.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtGenres.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtDuration.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtDirector.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtPlotOutline.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtTitle.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtReleaseDate.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtLangages.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtTraillers.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtCompanies.GotFocus += new RoutedEventHandler(control_GotFocus);
            txtCasts.GotFocus += new RoutedEventHandler(control_GotFocus);

            tbtRating1.Unchecked += new RoutedEventHandler(tbtRating_Unchecked);
            tbtRating1.Checked += new RoutedEventHandler(tbtRating_Checked);
            tbtRating2.Unchecked += new RoutedEventHandler(tbtRating_Unchecked);
            tbtRating2.Checked += new RoutedEventHandler(tbtRating_Checked);
            tbtRating3.Unchecked += new RoutedEventHandler(tbtRating_Unchecked);
            tbtRating3.Checked += new RoutedEventHandler(tbtRating_Checked);
            tbtRating4.Unchecked += new RoutedEventHandler(tbtRating_Unchecked);
            tbtRating4.Checked += new RoutedEventHandler(tbtRating_Checked);
            tbtRating5.Unchecked += new RoutedEventHandler(tbtRating_Unchecked);
            tbtRating5.Checked += new RoutedEventHandler(tbtRating_Checked);

            txtPath.MouseUp += new MouseButtonEventHandler(txtPath_MouseUp);

            _tbtRatings = new ToggleButton[5] { tbtRating1, tbtRating2, tbtRating3, tbtRating4, tbtRating5 };

            //---- Intellisense
            txtMetaTags.InitializeIntellisense(_intellisenseManager, 0);
            txtDirector.InitializeIntellisense(_intellisenseManager, 1);
            txtLangages.InitializeIntellisense(_intellisenseManager, 2);
            txtCasts.InitializeIntellisense(_intellisenseManager, 3);
            txtGenres.InitializeIntellisense(_intellisenseManager, 4);
            txtCompanies.InitializeIntellisense(_intellisenseManager, 5);
        }

        #endregion

        #region Refresh

        public void Refresh(List<Video> selectedVideos)
        {
            _selectedVideos = selectedVideos;
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                            (DelegateInvoker)delegate { AsyncRefresh(); });
        }

        private void AsyncRefresh()
        {
            _isUpdating = true;

            UpdateIntellisenseDB();

            try
            {
                //---- Reset
                _isReadOnly = Host.MediaService.CurrentMediaDeviceFeature.IsTagUpdateSupported;
                foreach (Video video in _selectedVideos)
                {
                    if (!_isReadOnly)
                        break;
                    _isReadOnly = _isReadOnly & Host.MediaService.CurrentMediaDeviceFeature.CanUpdateTag(video);
                }
                if (_selectedVideos.Count < 1)
                    _isReadOnly = false;

                _isReadOnly = !_isReadOnly;

                txtMetaTags.IsReadOnly = _isReadOnly;
                //btnMetaTags.IsEnabled = !_isReadOnly;
                txtTitle.IsReadOnly = _isReadOnly;
                txtDirector.IsReadOnly = _isReadOnly;
                txtLangages.IsReadOnly = _isReadOnly;
                txtCasts.IsReadOnly = _isReadOnly;
                txtGenres.IsReadOnly = _isReadOnly;
                txtTraillers.IsReadOnly = _isReadOnly;
                txtCompanies.IsReadOnly = _isReadOnly;
                txtDuration.IsReadOnly = true;
                txtReleaseDate.IsReadOnly = _isReadOnly;
                txtPlotOutline.IsReadOnly = _isReadOnly;
                tbtRating1.IsEnabled = !_isReadOnly;
                tbtRating2.IsEnabled = !_isReadOnly;
                tbtRating3.IsEnabled = !_isReadOnly;
                tbtRating4.IsEnabled = !_isReadOnly;
                tbtRating5.IsEnabled = !_isReadOnly;

                imgFrontCoverHighlight.IsEnabled = false;//!_isReadOnly;

                txtMetaTags.Text = "";
                txtTitle.Text = "";
                txtDirector.Text = "";
                txtLangages.Text = "";
                txtCasts.Text = "";
                txtGenres.Text = "";
                txtTraillers.Text = "";
                txtCompanies.Text = "";
                txtDuration.Text = "";
                txtReleaseDate.Text = "";
                txtPlotOutline.Text = "";
                txtPath.Text = "";
                tbtRating1.IsChecked = false;
                tbtRating2.IsChecked = false;
                tbtRating3.IsChecked = false;
                tbtRating4.IsChecked = false;
                tbtRating5.IsChecked = false;
                imgFrontCover.Source = null;

                if (_selectedVideos.Count < 1)
                    return;

                if (IsSameFieldValue(MediaDataTagField.MetaTags))
                    txtMetaTags.Text = _selectedVideos[0].FormattedMetaTags;
                if (IsSameFieldValue(MediaDataTagField.Title))
                    txtTitle.Text = _selectedVideos[0].Title;
                if (IsSameFieldValue(MediaDataTagField.Director))
                    txtDirector.Text = _selectedVideos[0].Director;
                if (IsSameFieldValue(MediaDataTagField.Languages))
                    txtLangages.Text = _selectedVideos[0].FormattedLanguages;
                if (IsSameFieldValue(MediaDataTagField.Casts))
                    txtCasts.Text = _selectedVideos[0].FormattedCasts;
                if (IsSameFieldValue(MediaDataTagField.Genres))
                    txtGenres.Text = _selectedVideos[0].FormattedGenres;
                if (IsSameFieldValue(MediaDataTagField.Traillers))
                    txtTraillers.Text = _selectedVideos[0].FormattedTraillers;
                if (IsSameFieldValue(MediaDataTagField.Companies))
                    txtCompanies.Text = _selectedVideos[0].FormattedCompanies;
                if (IsSameFieldValue(MediaDataTagField.Duration))
                    txtDuration.Text = _selectedVideos[0].FormattedDuration;
                if (IsSameFieldValue(MediaDataTagField.ReleaseDate))
                    txtReleaseDate.Text = _selectedVideos[0].FormattedReleaseDate;
                if (IsSameFieldValue(MediaDataTagField.PlotOutline))
                    txtPlotOutline.Text = _selectedVideos[0].PlotOutline;
                if (Host.MediaService.CurrentMediaDeviceFeature.IsManageFilePathSupported)
                {
                    lblPath.Visibility = Visibility.Visible;
                    txtPath.Visibility = Visibility.Visible;

                    if (IsSameFieldValue(MediaDataTagField.Path))
                        txtPath.Text = _selectedVideos[0].Path;
                }
                else
                {
                    lblPath.Visibility = Visibility.Hidden;
                    txtPath.Visibility = Visibility.Hidden;
                }

                if (IsSameFieldValue(MediaDataTagField.Rating))
                {
                    tbtRating1.IsChecked = _selectedVideos[0].IsRating1;
                    tbtRating2.IsChecked = _selectedVideos[0].IsRating2;
                    tbtRating3.IsChecked = _selectedVideos[0].IsRating3;
                    tbtRating4.IsChecked = _selectedVideos[0].IsRating4;
                    tbtRating5.IsChecked = _selectedVideos[0].IsRating5;
                }

                if (IsSameFieldValue(MediaDataTagField.Image))
                    imgFrontCover.Source = _selectedVideos[0].Thumbnail;
                else
                    imgFrontCover.Source = null;
            }
            finally
            {
                _isUpdating = false;
            }
        }

        #endregion

        #region Media Kernel Events

        void MediaKernel_MediasOperation(MediaFeature feature, MediaItem featureItem, MediaOperationKind operationKind, object parameter)
        {
            if (operationKind == MediaOperationKind.Removed)
                Refresh(new List<Video>());
        }

        #endregion

        #region IsSameFieldValue

        private bool IsSameFieldValue(MediaDataTagField field)
        {
            if (field == MediaDataTagField.Path)
            {
                if (_selectedVideos.Count == 1)
                    return true;

                return false;
            }

            Video previousVideo = _selectedVideos[0];
            for (int index = 1; index < _selectedVideos.Count; index++)
            {
                Video currentVideo = _selectedVideos[index];
                if (field == MediaDataTagField.MetaTags && currentVideo.FormattedMetaTags != previousVideo.FormattedMetaTags)
                    return false;
                if (field == MediaDataTagField.Title && currentVideo.Title != previousVideo.Title)
                    return false;
                if (field == MediaDataTagField.Genres && currentVideo.FormattedGenres != previousVideo.FormattedGenres)
                    return false;
                if (field == MediaDataTagField.Duration && currentVideo.FormattedDuration != previousVideo.FormattedDuration)
                    return false;
                if (field == MediaDataTagField.Rating && currentVideo.Rating != previousVideo.Rating)
                    return false;
                if (field == MediaDataTagField.Director && currentVideo.Director != previousVideo.Director)
                    return false;
                if (field == MediaDataTagField.PlotOutline && currentVideo.PlotOutline != previousVideo.PlotOutline)
                    return false;
                if (field == MediaDataTagField.ReleaseDate && currentVideo.FormattedReleaseDate != previousVideo.FormattedReleaseDate)
                    return false;
                if (field == MediaDataTagField.Languages && currentVideo.FormattedLanguages != previousVideo.FormattedLanguages)
                    return false;
                if (field == MediaDataTagField.Traillers && currentVideo.FormattedTraillers != previousVideo.FormattedTraillers)
                    return false;
                if (field == MediaDataTagField.Companies && currentVideo.FormattedCompanies != previousVideo.FormattedCompanies)
                    return false;
                if (field == MediaDataTagField.Casts && currentVideo.FormattedCasts != previousVideo.FormattedCasts)
                    return false;
                if (field == MediaDataTagField.FrontCoverPicture && !currentVideo.Thumbnail.Equals(previousVideo.Thumbnail))
                    return false;
            }

            return true;
        }

        #endregion

        #region GotFocus

        void control_GotFocus(object sender, RoutedEventArgs e)
        {
            if (sender is TextBox)
            {
                ((TextBox)sender).SelectAll();
                _previousTextValue = ((TextBox)sender).Text;
            }
            else
            {
                ((IntellisenseTextBox)sender).SelectAll();
                _previousTextValue = ((IntellisenseTextBox)sender).Text;
            }
        }

        #endregion

        #region LostFocus

        void control_LostFocus(object sender, RoutedEventArgs e)
        {
            if (_isUpdating)
                return;

            if (_selectedVideos == null)
                return;

            string newTextValue;

            if (sender is TextBox)
                newTextValue = ((TextBox)sender).Text.Trim();
            else
                newTextValue = ((IntellisenseTextBox)sender).Text.Trim();

            if (_previousTextValue.Equals(newTextValue))
                return;

            _isUpdating = true;

            MediaDataTagField field = MediaDataTagField.Title;
            object newValue = null;

            if (sender == txtMetaTags)
            {
                field = MediaDataTagField.MetaTags;
                newValue = txtMetaTags.Text.Trim();
            }
            else if (sender == txtTitle)
            {
                field = MediaDataTagField.Title;
                newValue = txtTitle.Text.Trim();
            }
            else if (sender == txtGenres)
            {
                field = MediaDataTagField.Genres;
                newValue = txtGenres.Text.Trim();
                //    try
                //    {
                //        newValue = Int32.Parse(txtTrackCount.Text.Trim());
                //    }
                //    catch { /* PUT IN RED */ }
            }
            else if (sender == txtDirector)
            {
                field = MediaDataTagField.Director;
                newValue = txtDirector.Text.Trim();
            }
            else if (sender == txtPlotOutline)
            {
                field = MediaDataTagField.PlotOutline;
                newValue = txtPlotOutline.Text.Trim();
            }
            else if (sender == txtReleaseDate)
            {
                field = MediaDataTagField.ReleaseDate;
                newValue = txtReleaseDate.Text.Trim();
            }
            else if (sender == txtLangages)
            {
                field = MediaDataTagField.Languages;
                newValue = txtLangages.Text.Trim();
            }
            else if (sender == txtTraillers)
            {
                field = MediaDataTagField.Traillers;
                newValue = txtTraillers.Text.Trim();
            }
            else if (sender == txtCompanies)
            {
                field = MediaDataTagField.Companies;
                newValue = txtCompanies.Text.Trim();
            }
            else if (sender == txtCasts)
            {
                field = MediaDataTagField.Casts;
                newValue = txtCasts.Text.Trim();
            }

            //---- Update intellisense
            UpdateIntellisenseDB();

            //---- Update
            List<MediaData> mediasToUpdate = new List<MediaData>(_selectedVideos.Count);
            foreach (MediaData media in _selectedVideos)
                mediasToUpdate.Add(media);
            Host.MediaService.UpdateMediaDatas(mediasToUpdate, Host.MediaService.CurrentMediaDeviceFeature, field, newValue);

            _isUpdating = false;
        }

        #endregion

        #region tbtRating_Checked

        void tbtRating_Checked(object sender, RoutedEventArgs e)
        {
            if (_isUpdating)
                return;

            if (_selectedVideos == null)
                return;

            _isUpdating = true;

            bool checkIt = false;
            for (int index = 4; index > -1; index--)
            {
                if (sender == _tbtRatings[index])
                    checkIt = true;
                _tbtRatings[index].IsChecked = checkIt;
            }

            int rating;
            if (tbtRating5.IsChecked == true)
                rating = 5;
            else if (tbtRating4.IsChecked == true)
                rating = 4;
            else if (tbtRating3.IsChecked == true)
                rating = 3;
            else if (tbtRating2.IsChecked == true)
                rating = 2;
            else if (tbtRating1.IsChecked == true)
                rating = 1;
            else
                rating = 0;

            //---- Update
            List<MediaData> mediasToUpdate = new List<MediaData>(_selectedVideos.Count);
            foreach (MediaData media in _selectedVideos)
                mediasToUpdate.Add(media);
            Host.MediaService.UpdateMediaDatas(mediasToUpdate, Host.MediaService.CurrentMediaDeviceFeature, MediaDataTagField.Rating, rating);

            _isUpdating = false;
        }

        void tbtRating_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_isUpdating)
                return;

            if (_selectedVideos == null)
                return;

            _isUpdating = true;

            bool checkIt = false;
            for (int index = 4; index > -1; index--)
            {
                if (sender == _tbtRatings[index])
                    checkIt = true;

                if (index == 0 && sender == _tbtRatings[0] && _tbtRatings[0].IsChecked == false)
                    _tbtRatings[0].IsChecked = false; // Inverse the value
                else
                    _tbtRatings[index].IsChecked = checkIt;
            }

            int rating;
            if (tbtRating5.IsChecked == true)
                rating = 5;
            else if (tbtRating4.IsChecked == true)
                rating = 4;
            else if (tbtRating3.IsChecked == true)
                rating = 3;
            else if (tbtRating2.IsChecked == true)
                rating = 2;
            else if (tbtRating1.IsChecked == true)
                rating = 1;
            else
                rating = 0;

            //---- Update
            List<MediaData> mediasToUpdate = new List<MediaData>(_selectedVideos.Count);
            foreach (MediaData media in _selectedVideos)
                mediasToUpdate.Add(media);
            Host.MediaService.UpdateMediaDatas(mediasToUpdate, Host.MediaService.CurrentMediaDeviceFeature, MediaDataTagField.Rating, rating);

            _isUpdating = false;
        }

        #endregion

        #region Click on file path

        void txtPath_MouseUp(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "explorer";

            Video video = _selectedVideos[0];

            string directory = new System.IO.FileInfo(video.Path).Directory.FullName;

            proc.StartInfo.Arguments = "\"" + directory + "\\\"";
            proc.Start();
        }

        #endregion

        #region Image drag & drop

        //void imgFrontCover_DragOver(object sender, DragEventArgs e)
        //{
        //    if (_isReadOnly)
        //    {
        //        e.Effects = DragDropEffects.None;
        //        e.Handled = true;
        //        return;
        //    }

        //    //---- Get data
        //    string[] imageFilePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
        //    if (imageFilePaths == null)
        //    {
        //        e.Effects = DragDropEffects.None;
        //        e.Handled = true;
        //        return;
        //    }

        //    foreach (string filePath in imageFilePaths)
        //    {
        //        FileInfo fileInfo = new FileInfo(filePath);
        //        string ext = fileInfo.Extension.ToLower();

        //        if (ext != ".gif" && ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".bmp")
        //        {
        //            e.Effects = DragDropEffects.None;
        //            e.Handled = true;
        //            return;
        //        }
        //    }

        //    //---- Ok, can handle it
        //    e.Effects = DragDropEffects.Copy;
        //    e.Handled = true;
        //}

        //void imgFrontCover_Drop(object sender, DragEventArgs e)
        //{
        //    if (_isReadOnly)
        //    {
        //        e.Effects = DragDropEffects.None;
        //        e.Handled = true;
        //        return;
        //    }

        //    //---- Get data
        //    string[] imageFilePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
        //    if (imageFilePaths == null)
        //    {
        //        e.Effects = DragDropEffects.None;
        //        e.Handled = true;
        //        return;
        //    }
        //    foreach (string filePath in imageFilePaths)
        //    {
        //        FileInfo fileInfo = new FileInfo(filePath);
        //        string ext = fileInfo.Extension.ToLower();

        //        if (ext != ".gif" && ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".bmp")
        //        {
        //            e.Effects = DragDropEffects.None;
        //            e.Handled = true;
        //            return;
        //        }
        //    }

        //    //---- Put the image in the video file
        //    UpdateTagImage(imageFilePaths[0]);

        //    //---- Set the right effect
        //    e.Effects = DragDropEffects.Copy;
        //    e.Handled = true;
        //}

        #endregion

        #region Image Dialog

        void imgFrontCover_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isReadOnly)
                return;

            if (e.ClickCount > 1)
                return;
            FileBrowserDialog dialog = new FileBrowserDialog();
            dialog.Filter = "Images files|*.gif;*.jpg;*.jpeg;*.png;*.bmp|All files (*.*)|*.*";
            //dialog.InitialDirectory = initialDirectory;
            dialog.Title = "Select an image file";
            dialog.Multiselect = false;
            dialog.CheckFileExists = true;

            //TvWindow.MainWindowWindowInteropHelper.Owner
            if (dialog.ShowDialog() != FileBrowserDialog.DialogResult.OK)
                return;

            UpdateTagImage(dialog.FileName);
        }

        #endregion

        #region Image update

        private void UpdateTagImage(string path)
        {
            //---- Put the image in the video file
            System.Drawing.Bitmap bitmap = System.Drawing.Bitmap.FromFile(path) as System.Drawing.Bitmap;

            //---- Update the image
            if (bitmap == null)
            {
                imgFrontCover.Source = null;
                return;
            }
            imgFrontCover.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                                                                        IntPtr.Zero,
                                                                        Int32Rect.Empty,
                                                                        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            //---- Set the image tags
            // convert...
            List<MediaData> mediasToUpdate = new List<MediaData>(_selectedVideos.Count);
            foreach (MediaData mediaData in _selectedVideos)
                mediasToUpdate.Add(mediaData);

            Host.MediaService.UpdateMediaDatas(mediasToUpdate,
                                                Host.MediaService.CurrentMediaDeviceFeature,
                                                MediaDataTagField.Image,
                                                bitmap);
        }

        #endregion

        #region UpdateIntellisenseDB

        private void UpdateIntellisenseDB()
        {
            _intellisenseManager.UpdateDB(Host.MediaService.CurrentMediaDeviceFeature.MediaDatas);
        }

        #endregion

    }

    #region VideoIntellisenseManager

    sealed public class VideoIntellisenseManager : ViewOnCore.Helper.WPF.IntellisenseManager
    {

        #region Constructor

        public VideoIntellisenseManager()
            : base(6)
        {
        }

        #endregion

        #region Visit

        override public void Visit(MediaData mediaData)
        {
            Video video = mediaData as Video;
            if (video == null)
                return;

            lock (UpdatingDatabases)
            {
                // Meta tags
                foreach (string key in video.MetaTags)
                    if (!UpdatingDatabases[0].ContainsKey(key))
                        UpdatingDatabases[0].Add(key, key);

                // Director
                if (!UpdatingDatabases[1].ContainsKey(video.Director))
                    UpdatingDatabases[1].Add(video.Director, video.Director);

                // Languages
                foreach (string key in video.Languages)
                    if (!UpdatingDatabases[2].ContainsKey(key))
                        UpdatingDatabases[2].Add(key, key);

                // Casts
                foreach (VideoCast cast in video.Casts)
                {
                    string key = cast.FirstName + " " + cast.LastName;
                    if (!UpdatingDatabases[3].ContainsKey(key))
                        UpdatingDatabases[3].Add(key, key);
                }

                // Genres
                foreach (string key in video.Genres)
                    if (!UpdatingDatabases[4].ContainsKey(key))
                        UpdatingDatabases[4].Add(key, key);

                // Companies
                foreach (string key in video.Companies)
                    if (!UpdatingDatabases[5].ContainsKey(key))
                        UpdatingDatabases[5].Add(key, key);
            }
        }

        #endregion

    }

    #endregion

}